package com.view.lyric
{
	import com.model.ModelLocator;
	import com.model.vo.KSCInfo;
	
	import mx.core.Application;
	import mx.effects.Tween;
	
	/**
	 * 
	 * ksc 文件解析
	 */	
	public class KSCLyricParser implements LyricParser
	{
		private var kscList:Array;
		private var kscRunList:Array = [];
		
		private var _model:ModelLocator;
		
		public function KSCLyricParser()
		{
			super();
			_model = ModelLocator.getInstance();
		}
		
		public function parse(data:String):Array
		{
			kscList = [];
			var info:String;
			var reg:RegExp = /'(.*?)'/g;
			var listArr:Array = data.split("\n");
			for (var i:int = 0; i < listArr.length; i++) 
			{
				if(listArr[i].indexOf("karaoke.add") != -1)
				{
					info = String(listArr[i]).substring(12, listArr[i].length-3);
					var arr:Array = [];
					arr = info.match(reg);
					
					var item:KSCInfo = new KSCInfo();
					item.startTimer = getLineInfo(arr[0]);  // 开始时间
					item.endTimer = getLineInfo(arr[1]);  // 结束时间
					item.lyric = arr[2].substring(1, arr[2].length-1);;  //该行歌词
					item.singleInfo = arr[3]; // 每个字的出现时间
					kscList.push(item);
		trace(item.startTimer, item.endTimer, item.lyric, item.lyric.length, item.singleInfo);
				}
			}
			kscList.sort(compare);
			return kscList;
			
		}
		
		private function compare(paraA:Object,paraB:Object):int 
		{
			if (paraA.startTimer > paraB.startTimer) {
				return 1;
			}
			if (paraA.startTimer < paraB.startTimer) {
				return -1;
			}
			return 0;
		}
		
		private function getLineInfo(str:String):Number
		{
			var reg:RegExp=/[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]/g;
			var timer:Number;
			var timeArr:Array = str.match(reg);
			for (var i:int = 0; i < timeArr.length; i++) 
			{
				var ctime:String=timeArr[i];
				// 计算时间
				timer = Number(ctime.substring(0,2))*60000+Number(ctime.substring(3,5))*1000 + Number(ctime.substring(6,9));
			}
			return timer;
			
		}
		
		public function getRunList(list:Array):Array
		{
			var temp:Array;
			var info:String;
			for (var i:int = 0; i < list.length; i++) 
			{
				info = list[i].singleInfo; 
				info = info.substring(1, info.length-1);
				temp = info.split(",");
				kscRunList.push(temp);
			}
			return kscRunList;
		}
		
		public function getTxtByPosition(now:Number):Object
		{
			var obj:Object;
			for (var i:int=0,j:int=kscList.length; i<j; i++) 
			{
				// 时间点在开始、结束之间
				if (now > kscList[i].startTimer && now < kscList[i].endTimer) 
				{
					obj = {
						currentLyric: kscList[i].lyric,
						nextLyric: i!=j-1 ? kscList[i+1].lyric : "",
						bool:i%2!=0
					};
					return obj;
				}
//				obj = {
//					currentLyric:"",
//					nextLyric:"",
////					currentLyric:kscList[j-1].lyric,
////					nextLyric:j<kscList.length ? kscList[j].lyric : "",
//					bool:j%2==0
//				}
			}
			return null;
		}
		
		public function getLengthByPosition(now:Number):Number
		{
			var len:Number = 0;
			var temp:Array;
			var info:String;
			var lineDuration:Number = 0; // 整行的停留时间
			for (var k:int = 0; k < kscList.length; k++) 
			{
				// 处于时间点内
				if(now > kscList[k].startTimer && now < kscList[k].endTimer)
				{
					temp = kscRunList[k];
					for (var i:int = 0; i < temp.length; i++) 
					{
						lineDuration += Number(temp[i]);
						if((now - kscList[k].startTimer) <= lineDuration)
						{
							// 表示现在处于哪一个字
							len += txtWidth*( (now - kscList[k].startTimer - (lineDuration - Number(temp[i])) )/Number(temp[i]) );
							
							if( Math.abs(len - temp.length*txtWidth)<10 )
							{
								len = temp.length*txtWidth; // seek 不能移动到最宽位置,  hack
							}
trace(i + "  ~~~~~~~~~~~~~~~!!~:  " + temp.length, kscList[k].lyric.charAt(i), (now - kscList[k].startTimer - (lineDuration - Number(temp[i]))), temp[i], 
	 (now - kscList[k].startTimer - (lineDuration - Number(temp[i])) )/Number(temp[i]), "\t" + len  );
							return len; 
						}
						len += txtWidth;
					}
					return len;
				}
				else
				{
					
				}
			}
			return 0;
		}
		
		public function seek(now:Number):void
		{
		}
		
		private var txtWidth:Number;
		public function topSeek(now:Number):void
		{
//			var dur:Number = (now - ModelLocator.getInstance().paiMaker.lastPosition)/1000;
//			txtWidth = _model.view.karaokeEditor.videoPlayer.topTxt.textWidth/_model.view.karaokeEditor.videoPlayer.topTxt.text.length;
////			trace("now: ", now);
//			_model.view.karaokeEditor.videoPlayer.topMaskImg.width = getLengthByPosition(now);
//			_model.view.karaokeEditor.videoPlayer.bottomMaskImg.width = 0;
////			TweenLite.to(ModelLocator.getInstance().paiMaker.videoPlayer.topMaskImg, dur, {width: getLengthByPosition(now)});
//			trace(now, " ----------top: ", ModelLocator.getInstance().paiMaker.videoPlayer.topMaskImg.width, 
//				ModelLocator.getInstance().paiMaker.videoPlayer.topTxt.textWidth,
//				ModelLocator.getInstance().paiMaker.videoPlayer.topTxt.width,
//				ModelLocator.getInstance().paiMaker.videoPlayer.topTxt.text,
//				ModelLocator.getInstance().paiMaker.videoPlayer.topTxt.textWidth/ModelLocator.getInstance().paiMaker.videoPlayer.topTxt.text.length
//			);
		}
		
		public function bottomSeek(now:Number):void
		{
			
//			var dur:Number = (now - ModelLocator.getInstance().paiMaker.lastPosition)/1000;
//			txtWidth = _model.view.karaokeEditor.videoPlayer.bottomTxt.textWidth/_model.view.karaokeEditor.videoPlayer.bottomTxt.text.length;
//			_model.view.karaokeEditor.videoPlayer.bottomMaskImg.width = getLengthByPosition(now);
//		trace("bottom: ", _model.view.karaokeEditor.videoPlayer.bottomMaskImg.width, txtWidth);
//			_model.view.karaokeEditor.videoPlayer.topMaskImg.width = 0;
////			TweenLite.to(ModelLocator.getInstance().paiMaker.videoPlayer.bottomMaskImg, dur, {width: getLengthByPosition(now)});
//			trace(now, "     bottom:--------- ",ModelLocator.getInstance().paiMaker.videoPlayer.bottomMaskImg.width,
//				ModelLocator.getInstance().paiMaker.videoPlayer.bottomTxt.textWidth,
//				ModelLocator.getInstance().paiMaker.videoPlayer.bottomTxt.width,
//				ModelLocator.getInstance().paiMaker.videoPlayer.bottomTxt.text,
//				ModelLocator.getInstance().paiMaker.videoPlayer.bottomTxt.textWidth/ModelLocator.getInstance().paiMaker.videoPlayer.bottomTxt.text.length
//			);
		}
		
		
	}
}